package com.nine.manage.service.impl;

import com.nine.common.constant.VmConstants;
import com.nine.common.utils.DateUtils;
import com.nine.common.utils.uuid.UUIDUtils;
import com.nine.manage.domain.Channel;
import com.nine.manage.domain.Node;
import com.nine.manage.domain.VendingMachine;
import com.nine.manage.domain.VmType;
import com.nine.manage.mapper.VendingMachineMapper;
import com.nine.manage.service.IChannelService;
import com.nine.manage.service.INodeService;
import com.nine.manage.service.IVendingMachineService;
import com.nine.manage.service.IVmTypeService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * 设备管理Service业务层处理
 *
 * @author nine
 * @date 2024-08-11
 */
@Service
public class VendingMachineServiceImpl implements IVendingMachineService {
	@Autowired
	private VendingMachineMapper vendingMachineMapper;

	@Autowired
	private IVmTypeService vmTypeService;

	@Autowired
	private INodeService nodeService;

	@Autowired
	private IChannelService channelService;

	/**
	 * 查询设备管理
	 *
	 * @param id 设备管理主键
	 * @return 设备管理
	 */
	@Override
	public VendingMachine selectVendingMachineById(Long id) {
		return vendingMachineMapper.selectVendingMachineById(id);
	}

	/**
	 * 查询设备管理列表
	 *
	 * @param vendingMachine 设备管理
	 * @return 设备管理
	 */
	@Override
	public List<VendingMachine> selectVendingMachineList(VendingMachine vendingMachine) {
		return vendingMachineMapper.selectVendingMachineList(vendingMachine);
	}

	/**
	 * 新增设备管理【涉及多表新增，添加事务处理】
	 *
	 * @param vendingMachine 设备管理
	 * @return 结果
	 */
	@Transactional
	@Override
	public int insertVendingMachine(VendingMachine vendingMachine) {
		// 1.新增设备
		// 1.1 生产8位唯一标识，补充货道编号
		String innerCode = UUIDUtils.getUUID();
		vendingMachine.setInnerCode(innerCode);
		// 1.2 查询售货机类型表，补充设备容量【设备容量为 售货机类型表 中冗余字段】
		VmType vmType = vmTypeService.selectVmTypeById(vendingMachine.getVmTypeId());
		if (vmType != null) {
			vendingMachine.setChannelMaxCapacity(vmType.getChannelMaxCapacity());
		}
		// 1.3 查询点位信息，补充区域、点位、合作商等信息
		Node node = nodeService.selectNodeById(vendingMachine.getNodeId());
		if (node != null) {
			// 第三个参数：需要忽略拷贝的字段
			BeanUtils.copyProperties(node, vendingMachine, "id");
			// 额外需要对应的字段，地址
			vendingMachine.setAddr(node.getAddress());
		}
		// 1.4 设备状态，0：未投放
		vendingMachine.setVmStatus(VmConstants.VM_STATUS_NODEPLOY);
		// 添加新增修改时间
		vendingMachine.setCreateTime(DateUtils.getNowDate());
		vendingMachine.setUpdateTime(DateUtils.getNowDate());
		int result = vendingMachineMapper.insertVendingMachine(vendingMachine);
		// 2.新增货道
		// 双层for循环
		if (vmType != null) {
			List<Channel> channelList = new ArrayList<>();
			for (int i = 1; i <= vmType.getVmRow(); i++) {
				for (int j = 1; j <= vmType.getVmCol(); j++) {
					// 1-1、1-2、1-3.....
					// 新增货道
					Channel channel = new Channel();
					// 售货机id
					channel.setVmId(vendingMachine.getId());
					// 货道编号
					channel.setChannelCode(i + "-" + j);
					// 售货机编号
					channel.setInnerCode(innerCode);
					// 货道最大容量
					channel.setMaxCapacity(vmType.getChannelMaxCapacity());
					channel.setCreateTime(DateUtils.getNowDate());
					channel.setUpdateTime(DateUtils.getNowDate());
					// 保存货道数据
					// channelService.insertChannel(channel);
					channelList.add(channel);
				}
			}
			channelService.batchInsertChannels(channelList);
		}
		return result;
	}

	/**
	 * 修改设备管理
	 *
	 * @param vendingMachine 设备管理
	 * @return 结果
	 */
	@Override
	public int updateVendingMachine(VendingMachine vendingMachine) {
		if (vendingMachine.getNodeId() != null) {
			// 1.3 查询点位信息，补充区域、点位、合作商等信息
			Node node = nodeService.selectNodeById(vendingMachine.getNodeId());
			if (node != null) {
				// 第三个参数：需要忽略拷贝的字段
				BeanUtils.copyProperties(node, vendingMachine, "id");
				// 额外需要对应的字段，地址
				vendingMachine.setAddr(node.getAddress());
			}
		}
		vendingMachine.setUpdateTime(DateUtils.getNowDate());
		return vendingMachineMapper.updateVendingMachine(vendingMachine);
	}

	/**
	 * 批量删除设备管理
	 *
	 * @param ids 需要删除的设备管理主键
	 * @return 结果
	 */
	@Override
	public int deleteVendingMachineByIds(Long[] ids) {
		return vendingMachineMapper.deleteVendingMachineByIds(ids);
	}

	/**
	 * 删除设备管理信息
	 *
	 * @param id 设备管理主键
	 * @return 结果
	 */
	@Override
	public int deleteVendingMachineById(Long id) {
		return vendingMachineMapper.deleteVendingMachineById(id);
	}


	/**
	 * 根据 设备编号innerCode 查询设备信息
	 *
	 * @param innerCode
	 * @return
	 */
	@Override
	public VendingMachine selectVendingMachineByInnerCode(String innerCode) {
		return vendingMachineMapper.selectVendingMachineByInnerCode(innerCode);
	}

}
